import java.util.*;
import java.util.stream.Collectors;

public class LambdaTest {
    public static void main(String[] args) {
//        List<String> list= Arrays.asList("Java","MySQL","Lambda","Spring");
//        //传统的循环并打印方法
//        for(String item:list){
//            System.out.println(item);
//        }
//        System.out.println();
//        //lambda
//        list.forEach((String item) -> {
//            System.out.println(item);
//        });

        //  interfaceTest();
        //   mapTest();
        //listSortTest();
        List<String > list=Arrays.asList("java","mysql","Spring","Lambda","Lambda","SpringBoot","Mybatis");
        //1.8之前
//        for (String item:list){
//            if(item.equals("Lambda")){
//                System.out.println("找到了");
//            }
//        }
        //1.8之后
        List<String> finds=list.stream().filter(s -> s.equals("Lambda")).collect(Collectors.toList());
        System.out.println(finds);

    }

    /**
     * list排序示例
     */
    private static void listSortTest() {
        //创建并初始化list
        List<Integer> list=Arrays.asList(5,3,2,7,9);
        System.out.println("排序前:"+list);
        list.sort((new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        }));
        System.out.println("排序后"+list);
    }


    /**
     * lambda循环使用示例
     */
    private static void mapTest() {
        HashMap<String,String> map=new HashMap<String,String>(){{
            put("java","java value");
            put("Mysql","mysql value");
        }};
        //jdk1.8之前
        for (Map.Entry<String,String> item: map.entrySet()) {
            System.out.println(item.getKey()+":"+item.getValue());
        }
        System.out.println();
        //lambda
        map.forEach((k,v)->{
            System.out.println(k+":"+v);
        });
    }


    /**
     * 函数式接口实例
     */
    private static void interfaceTest() {
        final int count=99;
        MyFunctionalInterface myFunctionalInterface=(o)->{
            for (Object item: o){
                System.out.println("item:"+item);
            }
            System.out.println("Count"+count);
        };
        myFunctionalInterface.myMethod("张三","李四","王五");
    }
}
